<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Common\Util\Page;

class MessageRecordController extends AdminbaseController {
	protected $where = array();
	
	protected $message_record_model;
	
	public function _initialize() {
		parent::_initialize();
		
		$this->message_record_model = M('message_notice_record');
		
		$type = I('get.type');
		$status = I('get.status');
		$mobile = I('get.mobile');
		
		if (isset($_GET['status']) && $status != -1) {
			$this->where ['status'] = $status;
		}
		
		if (isset($_GET['type']) && $type != -1) {
			$this->where ['type'] = $status == 'verification'?$status: array('neq', 'verification');
		}
		
		$starTime = I('get.start_time');
		if ($starTime) {
			$endTime = I('get.end_time');
			$this->where ['created'] = array(array('gt', strtotime($starTime.' 00:00:00')));
			if ($endTime) {
				$endTimeStamp = strtotime($endTime.' 23:59:59');
			} else {
				$endTimeStamp = time();
			}
			$this->where ['created'] [] = array('lt', $endTimeStamp);
		}
		
		if ($mobile) {
			$this->where ['mobile'] = array('eq', $mobile);
		}
		
// 		$this->where [] = "NOT_NULL(type)";
		$this->assign('url', U(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME));
	}
	
	public function index() {
		$count = $this->message_record_model->where($this->where)->count();
		
		$page = $this->page($count, 20);
		$list = $this->message_record_model
			->where($this->where)
			->order('created desc')
			->limit($page->firstRow.','.$page->listRows)
		->select ();
	
		$this->assign ( 'list', $list );
		$this->assign('Page', $page->show('Admin'));
		$this->display ('index');
	}
	
	public function verification() {
		$this->where ['type'] = 'verification';
		
		$count = $this->message_record_model->where($this->where)->count();
		
		$page = $this->page($count, 20);
		$list = $this->message_record_model
			->where($this->where)
			->order('created desc')
			->limit($page->firstRow.','.$page->listRows)
			->select();
		
		$this->assign ( 'list', $list );
		$this->assign('Page', $page->show('Admin'));
		$this->display ('index');
	}
	
	public function userPaySuccess() {
		if (! IS_POST) {
			$this->display ();
		} else {
			$Model = D ( 'MessageTemplate' );
			
			$data = I ( 'post.' );
			$c_id = 0;
			$data ['create_time'] = time();
			if ($c_id = $Model->add ( $data )) {
				$this->success ( '添加成功', U ( '/Admin/MessageTemplate/index' ) );
			} else {
				$this->error ( '添加失败' );
			}
		}
	}
}